import axios from "axios";


// 请求拦截器
axios.interceptors.request.use(config => {
    let jwt = sessionStorage.getItem("jwt");
    if(jwt) {
        config.headers.Authorization = jwt;
    }
    return config
})

// 相应拦截器
axios.interceptors.response.use(res => {
    let jwt = res.data.context ? res.data.context.jwt : null
    if(jwt) {
        sessionStorage.setItem("jwt", jwt)
    }
    let acl = res.data.context ? res.data.context.acl : null
    // console.log(acl);
    if(acl) {
        sessionStorage.setItem("acl", JSON.stringify(acl))
        let arr = []
        acl.forEach(el => {
            if(el.children.length > 0) {
                arr = [...arr, ...el.children]
            }
        })
        sessionStorage.setItem("bread", JSON.stringify(arr))
    }
    return res
})

export default axios